<?php

include_once 'Repost.php';
include_once 'UserDAO.php';

class Post {
	const TYPE_IMAGE = 'image';
	const TYPE_MOVIE = 'movie';
	const TYPE_COMMENT = 'comment';
	const TYPE_QUOTE = 'quote';
	const TYPE_ARTICLE = 'article';
	const TYPE_CSS = 'css';
	
  private $ownerId;  
	private $id;
	private $content;
	private $type;	
	private $source;
	private $title;
	private $postedData;
	private $isAuthor;
	private $isOriginal;
	private $listOfReposts;
	private $isRepost;
	private $prevPostOwner;
	private $originalPostOwner;
	private $originalOwner;
	private $prevOwner;
	private $originalPostId;
	private $show_title;
	private $image;
	private $description;
	private $tags;
	
	public function  __construct() {
		$this->show_title = $this->isAuthor = $this->isOriginal = false;
		$this->source = $this->image = null;
	}
	
	public function getTags() {
		return $this->tags;
	}
	
	public function setTags($t) {
		$this->tags=$t;			
	}
   
	public function getOwnerId() {
		return $this->ownerId;
	}
	
	public function setOwnerId($id) {
		$this->ownerId=$id;			
	}
   
	public function getUser() {
		return UserDAO::getInstance()->getUserById($this->getOwnerId());
	} 
	
	public function  getShowTitle() {
		return $this->show_title;
	}
	
	public function setShowTitle($show) {
		$this->show_title = (bool)$show;
	}
	
	public function  getImage() {
		return $this->image;
	}
	
	public function  setImage($q) {
		$this->image = $q;
	}
	
	public function  getDescription() {
		return $this->description;
	}
	
	public function  setDescription($d) {
		$this->description=$d;
	}
	
	public function  getId() {
		return $this->id;
	}
	
	public function  setId($id) {
		$this->id=$id;
	}
	
	public function getContent() {
		return $this->content;
	}
	
	public function setContent($content) {
		$this->content=$content;			
	}
	
	public function getSource() {
		return $this->source;
	}
	
	public function setSource($source) {
		$this->source=$source;			
	}
	
	public function getTitle() {
		return $this->title;
	}
	
	public function setTitle($title) {
		$this->title=$title;			
	}
	
	public function getDate() {
		return $this->postedData;
	}
	
	public function setDate($date) {
		$this->postedData = $date;			
	}
	
	public function isOriginal() {
		return $this->postedData;
	}
	
	public function setIsOriginal($is) {
		$this->postedData = (bool)$is;			
	}	
	
	public function isAuthor() {
		return $this->isAuthor;
	}
	
	public function setIsAuthor($is) {
		$this->isAuthor = (bool)$is;			
	}	
	
	public function getType() {
		return $this->type;
	}
	
	public function setType($type) {
		$this->type=$type;
	}
	
	public function getListOfReposts() {
		return $this->listOfReposts;
	}
	
	public function setListOfReposts($list) {
		$this->listOfReposts=$list;
	}
	
	public function isRepost() {
		return $this->isRepost;		
	}
	
	public function setIsRepost($is) {
		$this->isRepost=$is;
	}
	
	public function getprevPostOwner() {
		if (!isset($this->prevOwner)) {			
			$this->prevOwner=UserDAO::getInstance()->getUserById($this->prevPostOwner);			
		}
		return $this->prevOwner;
	}
	
	public function setprevPostOwner($owner) {
		$this->prevPostOwner=$owner;		
	}
	
	public function getOriginalPostOwner() {
		if (!isset($this->originalOwner)) {
			$this->originalOwner=UserDAO::getInstance()->getUserById($this->originalPostOwner);
		}
		return $this->originalOwner;
	}	
	
	public function setOriginalPostOwner($owner) {
		$this->originalPostOwner=$owner;
	}
	
	public function getprevOwner() {
		return $this->prevOwner;
	}
	
	public function setprevOwner($owner) {
		$this->prevOwner=$owner;		
	}
	
	public function getOriginalOwner() {
		return $this->originalOwner;
	}	
	
	public function setOriginalOwner($owner) {
		$this->originalOwner=$owner;
	}
	
	public function getOriginalPostId() {
		return $this->originalPostId;
	}	
	
	public function setOriginalPostId($owner) {
		$this->originalPostId=$owner;
	}
	
	public static function getPostByIdStatement() {
		return "SELECT * FROM post
				WHERE post_id=?";
	}
	
	public static function getRepostByIdStatement() {
		return "SELECT * FROM repost as r
				WHERE r.id=?";
	}
	
	public function getPostsStatement() {
		return "SELECT * FROM post as p 		
				JOIN post_content as pc ON p.post_content_id=pc.id 
				JOIN type as t on pc.type=t.type 
				WHERE p.posted <= (SELECT posted FROM post WHERE post_id=?) AND p.user_id=? ORDER BY p.posted DESC LIMIT ";
	}

	public function getRepostsStatement() {
		return "SELECT * FROM repost as r 
				JOIN post as p1 ON p1.repost_id=r.id				
				WHERE r.original_post_id IN (SELECT post_id FROM post as p2 WHERE p2.user_id=?)";		
	}
	
	public function getRepostsByUserStatement() {
		return "SELECT p1.post_id, p1.user_id, p2.post_content_id, pc.content, pc.source, 
					   pc.title, pc.is_author, t.name, r.prev_post_id, r.displayed_post_id, 
					   r.original_post_id, p1.posted, pc.show_title, pc.description, pc.image
					   FROM repost as r
				JOIN post as p1 ON r.id=p1.repost_id
				JOIN post as p2 ON r.original_post_id=p2.post_id
				JOIN post_content as pc on p2.post_content_id=pc.id
				JOIN type as t ON t.type=pc.type				
				WHERE p1.user_id=?";
	}
	
	public function getAllPostsStatement() {
		return "SELECT * FROM post as p
				LEFT JOIN post_content as pc ON p.post_content_id=pc.id
				LEFT JOIN type as t ON pc.type=t.type				
				ORDER BY p.posted DESC LIMIT 0 , ";
	}
	
	public function getNLatestPostsForUser() {
		return "SELECT * FROM post as p
				JOIN post_content as pc ON p.post_content_id=pc.id				
				JOIN type as t on pc.type=t.type WHERE p.user_id=? ORDER BY p.posted DESC LIMIT 0, ";
	}
	
	public function getNPostsBeforeDate () {
		return "SELECT * FROM post as p
				JOIN post_content as pc ON p.post_content_id=pc.id 
				JOIN type as t on pc.type=t.type 				
				WHERE p.posted <= (SELECT posted FROM Post WHERE post_id=?) ORDER BY p.posted DESC LIMIT 0, ";
	}
	
	public function getNReposts() {
		return "SELECT p2.post_id, p2.user_id, pc.content, p2.posted, pc.source, t.name, 
				pc.title, pc.show_title, pc.description, r.prev_post_id, r.displayed_post_id,
				pc.image, r.original_post_id FROM repost as r
				JOIN post as p ON r.original_post_id=p.post_id 
				JOIN post as p2 ON p2.repost_id=r.id
				JOIN post_content as pc ON p.post_content_id=pc.id				
				JOIN type as t ON pc.type=t.type LIMIT ";
	}
	
	public function getAddPostContentStatement() {
		return "INSERT INTO post_content(id,type,content,description,source,title,
		show_title,is_author,image) 	VALUES(?,?,?,?, ?, ?, ?, ?, ?)";
	}
	
	public static function getAddPostStatement() {
		return "INSERT INTO post(post_id,user_id,post_content_id,repost_id) VALUES(?,?,?,?)";
	}
	
	public static function getTagsStatement() {
		return "SELECT * FROM tag as t 
				join tags as ts ON t.id=ts.tag_id
				WHERE ts.post_id=?";
	}
	
	public static function getTagByContentStatement() {
		return "SELECT * FROM tag 
				WHERE tag_content=?";
	}
	
	public static function getAddTagStatement() {
		return "INSERT INTO tag(id,tag_content)
				VALUES(?,?)";
	}
	
	public static function getAddTagForPostStatement() {
		return "INSERT INTO tags(post_id,tag_id)
				VALUES(?,?)";
	}
	
	public static function getTagIdStatement() {
		return "SELECT * FROM tag
				WHERE tag_content=?";
	}
	
	public static function getTypeIdstatement() {
		return "select * from type where name=?";
	}
	
	public static function getDeletePostStatement() {
		//return 'DELETE post, post_content FROM post LEFT JOIN post_content ON post.post_content_id=post_content.id WHERE post.post_id=?'; //, repost
		return '
			DELETE post, post_content, repost 
			FROM post 
			LEFT JOIN post_content ON post.post_content_id=post_content.id
			LEFT JOIN repost ON post.repost_id=repost.id
			WHERE post.post_id=?
		'; //, repost
	}
	
	public static function getAddPostWithContentStatement() {
		return array(
			'INSERT INTO post_content VALUES(DEFAULT, (
				SELECT type FROM type WHERE type.name=?
			), ?, ?, ?, ?, ?, ?, ?)',
			'INSERT INTO post VALUES(DEFAULT, ?, LAST_INSERT_ID(), NULL, DEFAULT)'
		);
	}
	
	public static function _getAllPostsStatement() {
		return '
				SELECT
					post.post_id AS id, post.user_id AS user_id, post.posted AS posted, 
					pc.*, rpc.*, 
					pt.name AS type
				FROM post 
				LEFT JOIN post_content AS pc ON post.post_content_id=pc.id
				LEFT JOIN repost AS rep ON post.repost_id=rep.id
				LEFT JOIN post AS p ON post.repost_id=rep.id
				LEFT JOIN post_content AS rpc ON p.post_content_id=rpc.id
				JOIN type AS pt ON rpc.type=pt.type OR pc.type=pt.type
				LIMIT 0, ';
	}
	
	public static function getGetRepostsForPostStatement() {
		return "SELECT * FROM post as p
				JOIN repost as r ON r.id=p.repost_id
				JOIN user as u ON u.id=p.user_id
				WHERE r.original_post_id=?";
	}

	public static function getMaxImageWidth() {
		return Config::POST_IMAGE_MAX_WIDTH;
	}
}

?>